package cn.echo.hrm.service.impl;

import cn.echo.hrm.client.AuthServiceClient;
import cn.echo.hrm.domain.Employee;
import cn.echo.hrm.domain.Tenant;
import cn.echo.hrm.dto.LoginUserDto;
import cn.echo.hrm.dto.TenantEnteringDto;
import cn.echo.hrm.exception.Assert;
import cn.echo.hrm.exception.BusinessException;
import cn.echo.hrm.exception.ErrorCode;
import cn.echo.hrm.mapper.EmployeeMapper;
import cn.echo.hrm.mapper.TenantMapper;
import cn.echo.hrm.query.TenantQuery;
import cn.echo.hrm.service.ITenantService;
import cn.echo.hrm.util.AjaxResult;
import cn.echo.hrm.util.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxy
 * @since 2022-04-06
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {


    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AuthServiceClient authServiceClient;

    @Override
    public void entering(TenantEnteringDto enteringDto) {
        //参数校验
//        if (enteringDto==null)
//            throw new BusinessException("参数错误");
        //非空校验
//        Assert.assertNotNull(enteringDto,"参数错误");
//        Assert.assertNotNull(enteringDto.getEmployee(),"参数错误");
//        Assert.assertNotNull(enteringDto.getMealId(),"参数错误");
//        Assert.assertNotNull(enteringDto.getTenant(),"参数错误");
        //机构唯一性校验
        List<Tenant> tenants = tenantMapper.selectList(new EntityWrapper<Tenant>().eq("company_name", enteringDto.getTenant().getCompanyName()));
//        if (tenants!=null && tenants.size()>0)
//            throw new BusinessException("已经入驻，请直接登录");
        Assert.assertListNull(tenants, ErrorCode.CODE_400_TENANT_EXSIT);

        //管理员校验
        List<Employee> employees = employeeMapper
                .selectList(new EntityWrapper<Employee>().eq("username", enteringDto.getEmployee().getUsername()));
//        if (employees!=null && employees.size()>0)
//            throw new BusinessException("该管理员已经有机构了！请登录后添加新的机构");
        Assert.assertListNull(employees,ErrorCode.CODE_400_ADMIN_EXSIT);

        //远程调用auth feign接口添加loginUser维护套餐信息后返回loginId
        cn.echo.hrm.domain.LoginUser loginUser = new cn.echo.hrm.domain.LoginUser();
        loginUser.setUsername(enteringDto.getEmployee().getUsername());
        loginUser.setPassword(enteringDto.getEmployee().getPassword());
        loginUser.setType(0);

        LoginUserDto dto = new LoginUserDto();
        dto.setMealId(enteringDto.getMealId());
        dto.setLoginUser(loginUser);
        AjaxResult result = authServiceClient.add(dto);
//        if (!result.isSuccess())
//            throw new BusinessException("网络异常");
        Assert.assertTrue(result.isSuccess(),"网络异常");

        Long loginId = Long.valueOf(result.getResultObj().toString());

        //添加管理员信息并返回adminId
        Employee employee = enteringDto.getEmployee();
        employee.setLoginId(loginId);
        employeeMapper.insert(employee);

        //添加租户信息
        Long adminId = employee.getId();
        Tenant tenant = enteringDto.getTenant();
        tenant.setAdminId(adminId);
        tenantMapper.insert(tenant);

    }

    @Override
    public PageList<Tenant> selectPageList(Page<Tenant> page, TenantQuery query) {
        //查询到当前页数据
        List<Tenant> datas= tenantMapper.loadPageList(page,query);
        //满足条件放到page里面
        return new PageList<>(page.getTotal(),datas);
    }

}
